package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.ExInfo;
import com.example.demo.entity.SysUser;
import com.example.demo.exception.CustomException;
import com.example.demo.mapper.ExInfoMapper;
import com.example.demo.service.ExInfoService;
import com.example.demo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.*;

@RestController
@RequestMapping("/examine")
public class ExamineController extends BaseController{

    @Resource
    ExInfoMapper exInfoMapper;

    @Autowired
    ExInfoService exInfoService;

    @PostMapping
    public Result<?> save(@RequestBody ExInfo exInfo, HttpServletRequest request) {
        exInfo.setCreateTime(new Date());
        exInfoMapper.insert(exInfo);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody ExInfo exInfo,HttpServletRequest request) {
        SysUser user = TokenUtils.getUserInfo(request);
        if (user == null)
            return Result.error("500", "未获取到user信息");
        exInfo.setUpdateUser(user.getUsername());
        exInfo.setUpdateTime(new Date());
        exInfoMapper.updateById(exInfo);
        return Result.success();
    }

    @Transactional
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable @NotBlank String ids, HttpServletRequest request) {
        SysUser user = TokenUtils.getUserInfo(request);
        if (user == null)
            return Result.error("500", "未获取到user信息");
        List<String> idList = Arrays.asList(ids.split(","));
        for (String ID : idList) {
            ExInfo exInfo = exInfoMapper.selectById(ID);
            Optional.ofNullable(exInfo).orElseThrow(() ->  new CustomException("500001","该数据不存在"));
            exInfo.setUpdateUser(user.getUsername());
            exInfo.setUpdateTime(new Date());
            exInfoMapper.updateById(exInfo);
            exInfoMapper.deleteById(exInfo.getId());
        }
        return Result.success("删除成功");
    }


    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(exInfoMapper.selectById(id));
    }

/**
 * 
 * @author fqc
 * @date 2023/12/7 16:19
 * @param pageNum 页码
 * @param pageSize 数量
 * @param letterId 项目名称
 * @return com.example.demo.common.Result<?>
 */
    @GetMapping
    public Result<?> findPage(
//            @RequestParam(defaultValue = "1") Integer pageNum,
//                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") Integer letterId) {
        LambdaQueryWrapper<ExInfo> wrapper = Wrappers.<ExInfo>lambdaQuery();
        wrapper.eq(ExInfo::getLetterId, letterId);
//        Page<ExInfo> exInfoPage = exInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<ExInfo> exInfoList=exInfoMapper.selectList(wrapper);
        List<ExInfo> exInfoTree=exInfoService.convertToTree(exInfoList,0,null);
//        List<ExInfo> res=exInfoService.updateIsFinished(exInfoTree);
        return Result.success(exInfoTree);
    }

    @GetMapping("/list/{id}")
    public Result<?> list(@PathVariable Long id){
        List<ExInfo> timetables = exInfoMapper.selectAllByType(id);
        return Result.success(timetables);
    }

    @GetMapping("/list2/{id}")
    public Result<?> list2(@PathVariable Long id){
        List<ExInfo> timetables = exInfoMapper.selectAllBy2Type(id);
        return Result.success(timetables);
    }

}
